<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>CalcServlet.java</title>
<meta name="Generator" content="SciTE - www.Scintilla.org" />
<style type="text/css">
.S0 {
	color: #808080;
}
.S5 {
	font-weight: bold;
	color: #00007F;
}
.S6 {
	color: #7F007F;
}
.S10 {
	font-weight: bold;
	color: #000000;
}
span {
	font-family: 'Courier New';
	color: #000000;
	font-size: 9pt;
}
</style>
</head>
<body bgcolor="#FFFFFF">
<span><span class="S5">package</span><span class="S0"> </span>bbsimon<span class="S10">.</span>first<span class="S10">.</span>calc<span class="S10">;</span><br />
<br />
<span class="S5">import</span><span class="S0"> </span>java<span class="S10">.</span>io<span class="S10">.*;</span><br />
<span class="S5">import</span><span class="S0"> </span>javax<span class="S10">.</span>servlet<span class="S10">.*;</span><br />
<span class="S5">import</span><span class="S0"> </span>javax<span class="S10">.</span>servlet<span class="S10">.</span>http<span class="S10">.*;</span><br />
<span class="S5">import</span><span class="S0"> </span>bbsimon<span class="S10">.</span>first<span class="S10">.</span>model<span class="S10">.</span>calc<span class="S10">.*;</span><br />
<br />
<span class="S5">public</span><span class="S0"> </span><span class="S5">class</span><span class="S0"> </span>CalcServlet<span class="S0"> </span><span class="S5">extends</span><span class="S0"> </span>HttpServlet<span class="S0"> </span><span class="S10">{</span><br />
<span class="S0">&nbsp; &nbsp; &nbsp; &nbsp; </span><span class="S5">public</span><span class="S0"> </span><span class="S5">void</span><span class="S0"> </span>doGet<span class="S10">(</span>HttpServletRequest<span class="S0"> </span>req<span class="S10">,</span><span class="S0"> </span>HttpServletResponse<span class="S0"> </span>res<span class="S10">)</span><br />
<span class="S0">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </span><span class="S5">throws</span><span class="S0"> </span>IOException<span class="S10">,</span><span class="S0"> </span>ServletException<span class="S0"> </span><span class="S10">{</span><br />
<span class="S0">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </span>PrintWriter<span class="S0"> </span>out<span class="S0"> </span><span class="S10">=</span><span class="S0"> </span>res<span class="S10">.</span>getWriter<span class="S10">();</span><br />
<span class="S0">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </span>out<span class="S10">.</span>println<span class="S10">(</span><span class="S6">"&lt;html&gt;&lt;head&gt;&lt;title&gt;result&lt;/title&gt;"</span><br />
<span class="S0">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </span><span class="S10">+</span><span class="S0"> </span><span class="S6">"&lt;/head&gt;&lt;body&gt;"</span><span class="S10">);</span><br />
<span class="S0">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </span>String<span class="S0"> </span>s<span class="S0"> </span><span class="S10">=</span><span class="S0"> </span>req<span class="S10">.</span>getParameter<span class="S10">(</span><span class="S6">"number1"</span><span class="S10">);</span><br />
<span class="S0">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </span><span class="S5">int</span><span class="S0"> </span>num1<span class="S10">;</span><br />
<span class="S0">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </span><span class="S5">try</span><span class="S10">{</span><br />
<span class="S0">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </span>num1<span class="S0"> </span><span class="S10">=</span><span class="S0"> </span>Integer<span class="S10">.</span>parseInt<span class="S10">(</span>s<span class="S10">);</span><br />
<span class="S0">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </span><span class="S10">}</span><br />
<span class="S0">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </span><span class="S5">catch</span><span class="S10">(</span>NumberFormatException<span class="S0"> </span>nfe<span class="S10">){</span><br />
<span class="S0">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </span>out<span class="S10">.</span>println<span class="S10">(</span><span class="S6">"the first number you input is not a integer"</span><span class="S10">);</span><br />
<span class="S0">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </span>out<span class="S10">.</span>println<span class="S10">(</span><span class="S6">"&lt;/body&gt;&lt;/html&gt;"</span><span class="S10">);</span><br />
<span class="S0">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </span><span class="S5">return</span><span class="S10">;</span><br />
<span class="S0">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </span><span class="S10">}</span><br />
<span class="S0">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </span>s<span class="S0"> </span><span class="S10">=</span><span class="S0"> </span>req<span class="S10">.</span>getParameter<span class="S10">(</span><span class="S6">"number2"</span><span class="S10">);</span><br />
<span class="S0">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </span><span class="S5">int</span><span class="S0"> </span>num2<span class="S10">;</span><br />
<span class="S0">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </span><span class="S5">try</span><span class="S10">{</span><br />
<span class="S0">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </span>num2<span class="S0"> </span><span class="S10">=</span><span class="S0"> </span>Integer<span class="S10">.</span>parseInt<span class="S10">(</span>s<span class="S10">);</span><br />
<span class="S0">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </span><span class="S10">}</span><br />
<span class="S0">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </span><span class="S5">catch</span><span class="S10">(</span>NumberFormatException<span class="S0"> </span>nfe<span class="S10">){</span><br />
<span class="S0">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </span>out<span class="S10">.</span>println<span class="S10">(</span><span class="S6">"the second number you input is not a integer"</span><span class="S10">);</span><br />
<span class="S0">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </span>out<span class="S10">.</span>println<span class="S10">(</span><span class="S6">"&lt;/body&gt;&lt;/html&gt;"</span><span class="S10">);</span><br />
<span class="S0">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </span><span class="S5">return</span><span class="S10">;</span><br />
<span class="S0">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </span><span class="S10">}</span><br />
<br />
<span class="S0">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </span>CalcModel<span class="S0"> </span>calc<span class="S0"> </span><span class="S10">=</span><span class="S0"> </span><span class="S5">new</span><span class="S0"> </span>CalcModel<span class="S10">();</span><br />
<span class="S0">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </span><span class="S5">int</span><span class="S0"> </span>result1<span class="S0"> </span><span class="S10">=</span><span class="S0"> </span>calc<span class="S10">.</span>add<span class="S10">(</span>num1<span class="S10">,</span><span class="S0"> </span>num2<span class="S10">);</span><br />
<span class="S0">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </span><span class="S5">int</span><span class="S0"> </span>result2<span class="S0"> </span><span class="S10">=</span><span class="S0"> </span>calc<span class="S10">.</span>sub<span class="S10">(</span>num1<span class="S10">,</span><span class="S0"> </span>num2<span class="S10">);</span><br />
<span class="S0">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </span>out<span class="S10">.</span>println<span class="S10">(</span><span class="S6">"the result is &lt;br&gt;"</span><span class="S0"> </span><span class="S10">+</span><span class="S0"> </span>num1<span class="S0"> </span><span class="S10">+</span><span class="S0"> </span><span class="S6">" + "</span><span class="S0"> </span><span class="S10">+</span><span class="S0"> </span>num2<span class="S0"> </span><span class="S10">+</span><span class="S0"> </span><span class="S6">" = "</span><br />
<span class="S0">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </span><span class="S10">+</span><span class="S0"> </span>result1<span class="S0"> </span><span class="S10">+</span><span class="S0"> </span><span class="S6">"&lt;br&gt;"</span><span class="S10">);</span><br />
<span class="S0">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </span>out<span class="S10">.</span>println<span class="S10">(</span>num1<span class="S0"> </span><span class="S10">+</span><span class="S0"> </span><span class="S6">" - "</span><span class="S0"> </span><span class="S10">+</span><span class="S0"> </span>num2<span class="S0"> </span><span class="S10">+</span><span class="S0"> </span><span class="S6">" = "</span><br />
<span class="S0">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </span><span class="S10">+</span><span class="S0"> </span>result2<span class="S0"> </span><span class="S10">+</span><span class="S0"> </span><span class="S6">"&lt;br&gt;&lt;br&gt;&lt;a href=\"./\"&gt;click to go back&lt;/a&gt;&lt;/body&gt;&lt;/html&gt;"</span><span class="S10">);</span><br />
<span class="S0">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </span><span class="S10">}</span><br />
<span class="S10">}</span><br />
<span class="S0"></span></span>
</body>
</html>
